﻿using System.Configuration;
using System.Web.Configuration;
using System.Collections.Specialized;

namespace Sex
{
    public partial class Sex : System.Web.UI.Page
    {
        class ListConfiguration : MyList
        {
            void GetWebSection(Configuration conf, string SectionName)
            {
                AddTitle(SectionName, 2);
                SystemWebSectionGroup systemWeb = conf.GetSectionGroup(SectionName) as SystemWebSectionGroup;
                if (systemWeb != null)
                    for (int i = 0; i < systemWeb.Sections.Count; i++)
                    {
                        Add(systemWeb.Sections[i].SectionInformation.Name, GetSection(systemWeb.Sections[i]));
                    }
            }
            protected string GetSection(ConfigurationSection s)
            {
                if (s == null) return "";
                string KeyValuePairs = "";
                PropertyInformationCollection props = s.ElementInformation.Properties;
                foreach (PropertyInformation prop in props)
                {
                    string Name = prop.Name;
                    string Value = prop.Value as string; if (Value == null) Value = "";
                    KeyValuePairs += FormatMyKey(Name.ToString()) + FormatMyText("=") + Value + "<br/>";
                }
                return KeyValuePairs;
            }
            public ListConfiguration(State MyState)
                : base(MyState, "WebSettings")
            {
                AddTitle("Web Configuration AppSettings", 2);

                NameValueCollection nvc = WebConfigurationManager.AppSettings;
                for (int i = 0; i < nvc.Count; i++)
                {
                    Add(nvc.AllKeys[i], nvc[i]);
                }
                // HostingEnvironmentSection h = new HostingEnvironmentSection();
                // GetWebSection(h.CurrentConfiguration, "AppSettings");

                AddTitle("Web Configuration Connection Strings", 2);
                ConnectionStringSettingsCollection cssc = WebConfigurationManager.ConnectionStrings;
                for (int i = 0; i < cssc.Count; i++)
                {
                    ConnectionStringSettings conn = cssc[i];
                    Add(conn.Name.ToString(), conn.ConnectionString);
                }

                WebConfigurationFileMap wm = new WebConfigurationFileMap();
                Configuration c = WebConfigurationManager.OpenWebConfiguration("/"); //OpenMachineConfiguration(); 
                // GetWebSection(c, "appSettings");
                GetWebSection(c, "configSections");
                // GetWebSection(c, "system.codedom");
                GetWebSection(c, "system.web");
                // GetWebSection(c, "system.webServer");
                // GetWebSection(c, "runtime");

                // c = WebConfigurationManager.OpenMachineConfiguration();
                // GetWebSection(c, "runtime");

                // empty, not used, may be an syntax error
                // GetWebCachingSection(c);

                /*
                m = new MyList("Web Machine Configuration");
                ConfigurationSectionCollection css = c.Sections;
                for (int i = 0; i < css.Count; i++)
                {
                    ConfigurationSection cs = c.GetSection(css.Keys[i]);
                    string ss = "";
                    for (int j = 0; j < cs.ElementInformation.Properties.Count; j++)
                    {
                        string s = "";
                        if (cs.ElementInformation.IsCollection)
                        {
                            SectionInformation si = cs.SectionInformation;
                        }
                        PropertyInformationCollection pic = cs.ElementInformation.Properties;
                        foreach (PropertyInformation pi in pic)
                        {
                            switch (pi.Value.GetType().ToString())
                            {
                                case "SysteConfiguration.KeyValueConfigurationCollection":
                                    KeyValueConfigurationCollection fvsc = (KeyValueConfigurationCollection)pi.Value;
                                    for (int k = 0; k < fvsc.Count; k++)
                                    {
                                        s += fvsc.AllKeys[i] + "=" + fvsc[fvsc.AllKeys[i]].ToString();
                                    }
                                    break;
                                case "SysteConfiguration.ConnectionStringSettingsCollection":
                                    ConnectionStringSettingsCollection cssc1 = (ConnectionStringSettingsCollection)pi.Value;
                                    for (int k = 0; k < cssc1.Count; k++)
                                    {
                                        ConnectionStringSettings conn = cssc1[k];
                                        s += conn.Name + "=" + conn.ConnectionString;
                                    }
                                    break;
                                case "SysteString":
                                    s += pi.Name + "=" + pi.Value +"<br/>";
                                    break; 
                                case "SysteDiagnostics.AssertSection":
                                    // SysteDiagnostics.AssertSection sda_Value = (SysteDiagnostics)pi.Value;
                                    s += pi.Name + "=" + pi.Value +"<br/>";
                                    break;
                                case "SysteDiagnostics.PerfCounterSection":
                                    //SysteDiagnostics.PerfCounterSection sda_Value = (SysteDiagnostics)pi.Value;
                                    s += pi.Name + "=" + pi.Value + "<br/>";
                                    break;
                                case "SysteConfiguration.ProtectedProviderSettings":
                                    ProtectedProviderSettings Value = (ProtectedProviderSettings)pi.Value;
                                    s += pi.Name + "=" + pi.Value + "<br/>";
                                    break;
                                default:
                                    s += pi.Name + "=" + pi.Value + "<br/>";
                                    break;
                            }
                        }
                        ss+=s+" ";
                    }
                    Add(css.Keys[i], ss);
                }
                 */
            }
        }
    }
}